package xg2013.totoro.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import xg2013.totoro.domain.Attachment;
import xg2013.totoro.domain.Notice;
import xg2013.totoro.domain.Role;
import xg2013.totoro.domain.Tutor;
import xg2013.totoro.domain.User;
import xg2013.totoro.service.GetPresentUser;
import xg2013.totoro.service.GraduateProjectService;
import xg2013.totoro.service.NoticeService;
import xg2013.totoro.service.RoleService;
import xg2013.totoro.service.UserService;

@Controller
@RequestMapping("notice")
public class NoticeController {
	@Autowired
	private UserService userService;
	@Autowired
	private GraduateProjectService graduateProjectService;
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private RoleService roleService;

	@RequestMapping("/myNotice")
	public String myNoticeList(Model model, Integer role_id) {
		User user = userService.get(GetPresentUser.presentUserId);
		System.out.println(graduateProjectService.get(7).getTitle());
		Collection<Notice> noticeList = ((Tutor) user.getActor()).getNotices();
		model.addAttribute("noticeList", noticeList);
		model.addAttribute("role_id",role_id);
		return "notice/noticeList";
	}

	@RequestMapping(value = "/addNotice", method = RequestMethod.GET)
	public String addNoticePage(ModelMap modelMap,Integer role_id) {
		System.out.println("adsfsafas"+ role_id);
		modelMap.addAttribute("notice",new Notice());
		modelMap.addAttribute("attachment",new Attachment());
		List<Role> roleList = getRoleList(role_id);
		modelMap.addAttribute("receierList", roleList);
		return "notice/addNotice";
	}

	@RequestMapping(value = "/addNotice", method = RequestMethod.POST)
	public String addNotice(Notice notice,Integer issuerRole_id) {
		System.out.println("role_id" + issuerRole_id);
		// 添加user_id
		notice.setIssuer((Tutor) userService.get(GetPresentUser.presentUserId).getActor());
		// 添加提交时间
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		notice.setIssuedDate(cal);

		System.out.println("对象：" + notice);
		System.out.println("题目:" + notice.getTitle());
		System.out.println("时间：" + notice.getIssuedDate().getTime());
		System.out.println("描述:" + notice.getDescription());
		noticeService.save(notice);

		return "redirect:/notice/myNotice.html";

	}

	public List<Role> getRoleList(int id) {
		List<Role> roleList = new ArrayList<Role>();
		for (int i = id+1; i <= roleService.countAll(); i++) {
			Role role = roleService.get(i);
			System.out.println(role.getDescription());
			roleList.add(role);
		}
		return roleList;
	}

}
